package com.chuanjiabao.wisdom.ui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.application.FApplication;
import com.chuanjiabao.wisdom.bean.Constants;
import com.chuanjiabao.wisdom.databinding.ActivitySettingBinding;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.util.BHThreadPool;
import com.chuanjiabao.wisdom.util.LogUtil;

public class SettingActivity extends BaseActivity {

    ActivitySettingBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_setting);
        binding.setActivity(this);
        initView();

    }

    private void initView() {
        setBack();
        setTitleText("设置");

        binding.webView.getSettings().setDomStorageEnabled(true);
        binding.webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        String appCachePath = FApplication.getInstance().getCacheDir()
                .getAbsolutePath();
        binding.webView.getSettings().setAppCachePath(appCachePath);
        binding.webView.getSettings().setAllowFileAccess(true);
        binding.webView.getSettings().setAppCacheEnabled(true);
        binding.webView.getSettings().setJavaScriptEnabled(true);
        binding.webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        binding.webView.addJavascriptInterface(SettingActivity.this, "android");
        binding.webView.setWebViewClient(new MyWebViewClient());
        binding.webView.loadUrl(Constants.URL_HTML_ABOUT_BIHU);
        binding.webView.setBackgroundColor(getResources().getColor(R.color.transparent));
        binding.webView.getSettings().setSupportZoom(true);
        binding.webView.getSettings().setBuiltInZoomControls(true);
        binding.webView.getSettings().setDisplayZoomControls(false);

        binding.llModuleOut.setOnClickListener(this);
    }

    public class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageFinished(WebView view, String url) {
            LogUtil.e("web界面加载完毕");
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.llModuleOut:
                break;
                default:
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding.webView.clearHistory();
        binding.webView.clearCache(true);
        binding.webView.loadUrl("about:blank");
        binding.webView.freeMemory();
        binding.webView.pauseTimers();
    }

    @JavascriptInterface
    public void familyDetail(final String userId) {
        LogUtil.e("js调用familyDetail");
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                LogUtil.e("家庭详情点击-->" + userId);
            }
        };
        BHThreadPool.getInstance().executeTask(runnable);
    }

    @JavascriptInterface
    public void addFamilyBody(final String x, final String y) {
        LogUtil.e("js调用addFamilyBody");
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                LogUtil.e("家庭成员添加-->" + x + "   " + y);
            }
        };
        BHThreadPool.getInstance().executeTask(runnable);
    }


}
